package vn.lumi.smarthome.load;

import vn.lumi.smarthome.io.database.TBL_Device;
import vn.lumi.smarthome.io.database.TBL_DeviceScene;
import vn.lumi.smarthome.io.database.TBL_DeviceState;
import vn.lumi.smarthome.io.model.Device;
import vn.lumi.smarthome.io.model.DeviceState;
import android.content.Context;
import android.database.Cursor;
import android.support.v4.content.AsyncTaskLoader;
import android.util.Log;

public class DeviceListLoader extends AsyncTaskLoader<Device[]>{
	private int mRoomID;
	private TYPE mType;
	private int mSceneID;
	
	public enum TYPE{
		CREATE,
		EDIT
	}
	
	public DeviceListLoader(Context context,TYPE type,int room_id) {
		super(context);
//		mFloorID=floor_id;
		mRoomID=room_id;
		mType=type;
	}
	public DeviceListLoader(Context context,TYPE type, int roomID, int sceneID) {
		super(context);
		mType=type;
		mSceneID=sceneID;
		mRoomID = roomID;
	}

	@Override
	public Device[] loadInBackground() {
		Device[] devices=null;
		if(mType==TYPE.CREATE)
			devices=loadDeviceListForCreateScene(mRoomID);
		else if(mType==TYPE.EDIT){
			devices=loadDeviceListForEditScene(mSceneID, mRoomID);
		}
		return devices;
	}
	
	private Device[] loadDeviceListForEditScene(int sceneID, int roomID){
		Log.d("DeviceListLoader","Load devices for sceneID="+sceneID);
		Device[] roomDevices=null;
		String selection = TBL_Device.ROOM_ID +"=?";
		String[] selectionArgs=new String[]{roomID+""};
		String[] projections=new String[]{TBL_Device._ID,TBL_Device.NAME,TBL_Device.TYPE_ID};
		Cursor cursor=getContext().getContentResolver().query(TBL_Device.CONTENT_URI_DEVICES, projections, selection, selectionArgs, null);
		if(cursor==null) return null;
		roomDevices=new Device[cursor.getCount()];
		int i=0;
		for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()){
			String name=cursor.getString(cursor.getColumnIndex(TBL_Device.NAME));
			int device_id=cursor.getInt(cursor.getColumnIndex(TBL_Device._ID));
			int type_id=cursor.getInt(cursor.getColumnIndex(TBL_Device.TYPE_ID));
			
			Device device=new Device(device_id, type_id, roomID, name, 0, new DeviceState(), false);
			roomDevices[i]=device;
			i++;
		}
		
		selection=TBL_DeviceScene.SCENE_ID+"=?";
		selectionArgs=new String[]{sceneID+""};
		cursor=getContext().getContentResolver().query(TBL_DeviceScene.CONTENT_URI_DEVICE_SCENE, null, selection, selectionArgs, null);
		if(cursor==null) return roomDevices;
		for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()){
			int device_id=cursor.getInt(cursor.getColumnIndex(TBL_DeviceScene.DEVICE_ID));
			int rgb = cursor.getInt(cursor.getColumnIndex(TBL_DeviceState.RGB));
			int value = cursor.getInt(cursor.getColumnIndex(TBL_DeviceState.VALUE));
			int blind_state = cursor.getInt(cursor.getColumnIndex(TBL_DeviceState.BLIND_STATE));

			Log.d("DeviceListLoader","device in scene: id="+device_id + " value="+value+" rgb="+rgb);
			
			for(Device device:roomDevices){
				if(device.isSelected()) continue;
				if(device_id == device.getId()){
					device.setSelected(true);
					device.getDeviceState().setBlind_state(blind_state);
					device.getDeviceState().setRGB(rgb);
					device.getDeviceState().setValue(value);
					break;
				}
			}
		}
			
		return roomDevices;
	}
	private Device[] loadDeviceListForCreateScene(int roomID){
		Device[] devices=null;
		
		String selection=TBL_Device.ROOM_ID+"=?";
		String[] selectionArgs=new String[]{roomID+""};
		String[] projections=new String[]{TBL_Device._ID,TBL_Device.NAME,TBL_Device.TYPE_ID};
		
		Cursor cursor=getContext().getContentResolver().query(TBL_Device.CONTENT_URI_DEVICES, projections, selection, selectionArgs, null);
		if(cursor==null) return null;
		devices=new Device[cursor.getCount()];
		int i=0;
		for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()){
			String name=cursor.getString(cursor.getColumnIndex(TBL_Device.NAME));
			int device_id=cursor.getInt(cursor.getColumnIndex(TBL_Device._ID));
			int type_id=cursor.getInt(cursor.getColumnIndex(TBL_Device.TYPE_ID));
			
			Device device=new Device(device_id, type_id, roomID, name, 0, new DeviceState(), false);
			devices[i]=device;
			i++;
		}
		return devices;
	}

}
